/**
 * @author Michael
 */

var date=new Date();
var view="calendar";
var period="month";

var months= new Array("January","February","March","April","May","June","July","August","September","October","November","December");


function viewEvent()
{
	arrEventID = $(this).attr('id').split('-');
	EventID=arrEventID.pop();
	$("#calendar-month-event-edit").slideUp(1000);
	$("#calendar-month-event-view").slideUp(1000);
	$.post
	('model/ajax/event-view.php',
		{
			EventID:EventID,
			UserID:UserID,
			SessionID:SessionID
		},
		viewEventComplete
	)

}

function viewEventComplete(data)
{
	if(data=="ERROR_NOT_LOGGED_ON")
	{
		$("#calendar-month-event-view").slideUp(1000);		
		$("#diary").slideUp(1000);		
		$("#logon-message").html("You are no longer logged in - please enter your username and password");
		$("#logon-message").slideDown(800);
//		checkAuthentication(false);
	}
	else
	{
		$("#calendar-month-event-view").html(data);
		$("#calendar-month-event-view").slideDown(1000);
		$("#calendar-month-event-view").draggable();
		$("#calendar-month-event-view-close").hover(function(){$(this).addClass("calendar-month-event-view-close-hover");},function(){$(this).removeClass("calendar-month-event-view-close-hover");});
		$("#calendar-month-event-view-close").click(function(){$("#calendar-month-event-view").slideUp(1000);});
		$("#calendar-month-event-view-edit-button").hover(function(){$(this).addClass("calendar-month-event-view-edit-button-hover");},function(){$(this).removeClass("calendar-month-event-view-edit-button-hover");});
		$("#calendar-month-event-view-edit-button").click(function(){editDiaryEvent();});
	}
}

function newEvent()
{
//	alert(url);
	$("#calendar-month-event-view").slideUp(1000);
	hiddenID="#"+$(this).attr("id")+" input";
	$.post
	('model/ajax/event-edit.php',
		{
			EventID:-1,
			Date:$(hiddenID).val(),
			UserID:UserID,
			SessionID:SessionID
		},
		editDiaryEventComplete
	)
}

function editDiaryEvent()
{
//	alert(url);
	$("#calendar-month-event-view").slideUp(1000);
	$.post
	('model/ajax/event-edit.php',
		{
			EventID:$("#calendar-month-event-view-id").html(),
			UserID:UserID,
			SessionID:SessionID
		},
		editDiaryEventComplete
	)
}

function editDiaryEventComplete(data)
{
	if(data=="ERROR_NOT_LOGGED_ON")
	{
		$("#calendar-month-event-edit").slideUp(1000);		
		$("#diary").slideUp(1000);		
		$("#logon-message").html("You are no longer logged in - please enter your username and password");
		$("#logon-message").slideDown(800);
//		checkAuthentication(false);
	}
	else
	{
		$("#calendar-month-event-edit").html(data);
		$("#calendar-month-event-edit").slideDown(1000);
		$("#calendar-month-event-edit").draggable();
		$("#calendar-month-event-edit-close").hover(function(){$(this).addClass("calendar-month-event-edit-close-hover");},function(){$(this).removeClass("calendar-month-event-edit-close-hover");});
		$("#calendar-month-event-edit-booking-add").click(function(){loadBooking();});
		$("#calendar-month-event-edit-booking-add").hover(function(){$(this).addClass("calendar-month-event-edit-booking-add-hover");},function(){$(this).removeClass("calendar-month-event-edit-booking-add-hover");});

		$("#calendar-month-event-edit-booking-word").click(function()
		{
			window.open("views/documents/word-booking-form.php?formID="+$("#calendar-month-event-edit-eventid").val());
		});
		$("#calendar-month-event-edit-booking-word").hover(function(){$(this).addClass("calendar-month-event-edit-booking-word-hover");},function(){$(this).removeClass("calendar-month-event-edit-booking-word-hover");});

		$("#calendar-month-event-edit-close").click(function(){$("#calendar-month-event-edit").slideUp(1000);});
		$(".calendar-month-event-edit-buttons").hover(function(){$(this).addClass("calendar-month-event-edit-buttons-hover");},function(){$(this).removeClass("calendar-month-event-edit-buttons-hover");});
		$("#calendar-month-event-edit-update-button").click(function(){updateEvent();});
		$("#calendar-month-event-edit-insert-button").click(function(){insertEvent();});
		$("#calendar-month-event-edit-delete-button").click(function(){deleteEvent();});
		$("#calendar-month-event-edit-cancel-button").click(function(){$("#calendar-month-event-edit").slideUp(1000);});
		$("#calendar-month-event-edit-date").datepicker({dateFormat:'yy-mm-dd', firstDay: 1});
		$("#calendar-month-event-edit-start").timepicker({});
		$("#calendar-month-event-edit-end").timepicker({});
		$("#calendar-month-event-edit-repeatuntil").datepicker({dateFormat:'yy-mm-dd', firstDay: 1});	
		if($("#calendar-month-event-edit-eventid").val()=="")
		{
			$("#calendar-month-event-edit-update-button").hide();
			$("#calendar-month-event-edit-delete-button").hide();
		}
		
		if($("#calendar-month-event-edit-allday").is(':checked'))$("#calendar-month-event-edit-times").hide();
		$("#calendar-month-event-edit-allday").change(alldayChange);
		$("#calendar-month-event-edit-date").change(startDateChange);
	}
}

function startDateChange() // need to reset repeat until so it is 
{
	$("#calendar-month-event-edit-repeatuntil").val($("#calendar-month-event-edit-date").val())
}

function alldayChange()
{
		if($("#calendar-month-event-edit-allday").is(':checked'))
		{
			$("#calendar-month-event-edit-times").fadeOut(1000);
			$("#calendar-month-event-edit-start").val("00:00");
			$("#calendar-month-event-edit-end").val("00:00");
		}
		else
		{
			$("#calendar-month-event-edit-times").fadeIn(1000);
			hour = date.getHours();
			if (hour.length==1) hour = "0" + hour;
			min = date.getMinutes();
			if (min.length==1) min = "0" + min;
			strTime = hour +":" + min;
			$("#calendar-month-event-edit-start").val(strTime);
			$("#calendar-month-event-edit-end").val(strTime);
		}
}

function deleteEvent()
{
	r = confirm("Are you sure you want to delete this event?");
	if(r==true)
	{
		$.post
		('model/ajax/event-edit-process.php',
			{
				action:"delete",
				eventid:$("#calendar-month-event-edit-eventid").val(),
				UserID:UserID,
				SessionID:SessionID
			},
			deleteEventSuccess
		)
	}
}

function deleteEventSuccess(data)
{
	if(data=="ERROR_NOT_LOGGED_ON")
	{
//		$("#calendar-month-event-view").slideUp(1000);		
		$("#diary").slideUp(1000);	
			
		$("#logon-message").html("You are no longer logged in and your event was not deleted - please enter your username and password");
		$("#logon-message").slideDown(800);
//		checkAuthentication(false);
	}
	else
	{
		$("#calendar-month-event-edit").slideUp(1000);
		loadDiary();
		loadReports();	
	}
}


function updateEvent()
{
//	alert ("updateEvent()");
	$.post
	('model/ajax/event-edit-process.php',
		{
			action:"update",
			eventid:$("#calendar-month-event-edit-eventid").val(),
			originalrecordid:$("#calendar-month-event-edit-originalrecordid").val(),
			date:$("#calendar-month-event-edit-date").val(),
			start:$("#calendar-month-event-edit-start").val(),
			end:$("#calendar-month-event-edit-end").val(),
			title:$("#calendar-month-event-edit-title-text").val(),
			description:$("#calendar-month-event-edit-description-text").val(),
			comment:$("#calendar-month-event-edit-comment-text").val(),
			responsible:$("#calendar-month-event-edit-responsible-text").val(),
			eventtype:$("#calendar-month-event-edit-type-text").val(),
			locations:$("#calendar-month-event-edit-locations-text").val(),
			involvees:$("#calendar-month-event-edit-involvees-text").val(),
			repeatevery:$("#calendar-month-event-edit-multi-repeatoptions").val(),
			repeatuntil:$("#calendar-month-event-edit-repeatuntil").val(),
			UserID:UserID,
			SessionID:SessionID
		},
		updateEventSuccess
	)
}

function updateEventSuccess(data)
{
	if(data=="ERROR_NOT_LOGGED_ON")
	{
		$("#calendar-month-event-edit").slideUp(1000);		
		$("#diary").slideUp(1000);		
		$("#logon-message").html("You are no longer logged in and your event has not been updated - please enter your username and password");
		$("#logon-message").slideDown(800);
//		checkAuthentication(false);
	}
	else
	{
//		alert(data);
		$("#calendar-month-event-edit").slideUp(1000);
		loadDiary();
		loadReports();	
	}
}

function insertEvent()
{
//	alert ("insertEvent()");
	
	$.post
	('model/ajax/event-edit-process.php',
		{
			action:"insert",
			eventid:$("#calendar-month-event-edit-eventid").val(),
			originalrecordid:$("#calendar-month-event-edit-originalrecordid").val(),
			date:$("#calendar-month-event-edit-date").val(),
			start:$("#calendar-month-event-edit-start").val(),
			end:$("#calendar-month-event-edit-end").val(),
			title:$("#calendar-month-event-edit-title-text").val(),
			description:$("#calendar-month-event-edit-description-text").val(),
			comment:$("#calendar-month-event-edit-comment-text").val(),
			responsible:$("#calendar-month-event-edit-responsible-text").val(),
			eventtype:$("#calendar-month-event-edit-type-text").val(),
			locations:$("#calendar-month-event-edit-locations-text").val(),
			involvees:$("#calendar-month-event-edit-involvees-text").val(),
			repeatevery:$("#calendar-month-event-edit-multi-repeatoptions").val(),
			repeatuntil:$("#calendar-month-event-edit-repeatuntil").val(),
			UserID:UserID,
			SessionID:SessionID
		},
		insertEventSuccess
	)
}

function insertEventSuccess(data)
{
	if(data=="ERROR_NOT_LOGGED_ON")
	{
		$("#calendar-month-event-edit").slideUp(1000);		
		$("#diary").slideUp(1000);		
		$("#logon-message").html("You are no longer logged in and your event has not been inserted - please enter your username and password");
		$("#logon-message").slideDown(800);
//		checkAuthentication(false);
	}
	else
	{
//		alert(data);
		$("#calendar-month-event-edit").slideUp(1000);
		loadDiary();
		loadReports();	
		}
}

function getPHPFormatDate(aDate)
{
	year = aDate.getFullYear();
	month = aDate.getMonth()+1;
	day = aDate.getDate();
	return year+"-"+month+"-"+day;
}

function arrowClick()
{
	switch($(this).text())
	{
		case '<':
			date.setMonth(date.getMonth()-1);
			break;
		case '>':
			date.setMonth(date.getMonth()+1);
			break;
		case '<<':
			date.setYear(date.getFullYear()-1);
			break;
		case '>>':
			date.setYear(date.getFullYear()+1);
			break;
	}
	loadDiary();
}

function checkDiaryRowHeight()
{
	$(".calendar-month-row").each(function()
	{
		var newHeight=$(this).height();
		$(this).children(".calendar-month-cell").each(function()
		{
			$(this).height(newHeight);
		});
	});	
}


function onceLoadedDiary()
{
	$("#diary").slideDown(2000);
	checkDiaryRowHeight();
	$(".calendar-month-cell div.calendar-month-event-container").hover(function(){$(this).addClass("calendar-month-event-container-hover");},function(){$(this).removeClass("calendar-month-event-container-hover");});
	$("div.diary-control-arrow").hover(function(){$(this).addClass("diary-control-arrow-hover");},function(){$(this).removeClass("diary-control-arrow-hover");});
	$(".calendar-month-cell div.calendar-month-event").click(viewEvent);
	$(".calendar-month-date").hover(function(){$(this).addClass("calendar-month-date-hover");},function(){$(this).removeClass("calendar-month-date-hover");});
	$(".calendar-month-date").click(newEvent)

}

function loadDiary()
{
	switch(view)
	{
		case "calendar":
		default:
			switch (period)
			{
				case "month":
				default:
					$("#diary-control-month").html(months[date.getMonth()]+' '+(date.getFullYear()));
					url='model/ajax/diary.php?view='+view+'&period='+period+'&date='+getPHPFormatDate(date)+'&SessionID='+SessionID+'&UserID='+UserID;
					$("#calendar-month").load(url,onceLoadedDiary);
					break;
			}
			break;
	}
}

